package com.imoocdl.controller;

import com.imoocdl.dto.OrderDtDTO;
import com.imoocdl.enums.ResultEnum;
import com.imoocdl.exception.SellException;
import com.imoocdl.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 卖家端订单
 * Created by pc on 2018/7/23.
 */
@Controller
@RequestMapping(value = "/seller/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderService orderService;
    /**
     *
     * @param page 第几页 (默认从第一页开始)
     * @param size 每页条目
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                 @RequestParam(value = "size",defaultValue = "4") Integer size
                                , Map<String,Object> map){
        PageRequest pageRequest = new PageRequest(page-1,size);
        Page<OrderDtDTO> orderDtDTOPage =  orderService.findList(pageRequest);
        map.put("orderDtDTOPage",orderDtDTOPage);
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("order/list",map);
    }
    @GetMapping(value = "/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId,Map<String,Object> map){

        try {
            OrderDtDTO orderDtDTO = orderService.findOne(orderId);
            orderService.cancel(orderDtDTO);
        }catch (SellException e){
            log.error("[卖家取消订单] 发生异常");
            map.put("msg", e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg",ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success");
    }
    /**
     * 订单详情
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId
                        ,Map<String,Object> map){
        OrderDtDTO orderDtDTO = new OrderDtDTO();
        try {
            orderDtDTO = orderService.findOne(orderId);
        }catch (SellException e){
            log.error("卖家查询订单详情 发生异常{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("orderDtDTO",orderDtDTO);
        return new ModelAndView("pay/detail");
    }
    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam("orderId") String orderId
            ,Map<String,Object> map){
        try{
            OrderDtDTO orderDtDTO = orderService.findOne(orderId);
            orderService.finish(orderDtDTO);
        }catch (SellException e){
            log.error("[卖家完结订单] 发生异常");
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg",ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success");
    }
}
